package Simple2;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class ArrayHoppers {
	private static String resultsString = "";
	public static String getminhop(int[] inputarray){
		int start = 0;
		int firstelement = inputarray[start];
		return hopbrain(start,firstelement, inputarray);
	}
	
	
	public static int[] getfiles(String filereader) throws IOException{
		FileReader reader;
		ArrayList<String> templist = new ArrayList<String>();
		try {
			reader = new FileReader(filereader);
			BufferedReader br = new BufferedReader(reader);
			  String row;
			   while((row = br.readLine())!=null){
			    templist.add(row);
			   }
			   int[] inputarray = new int[templist.size()];
			   for (int i = 0; i < templist.size(); i++) {
				   inputarray[i] = Integer.parseInt(templist.get(i));
			}
			   reader.close();
			   br.close();
			   return inputarray;  
			   
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;  
		
	}
	
	public static String hopbrain(int privot, int range, int[] sourcearray){
		int[] arraytemp = new int[range];
		int maxindex = 0;
		maxindex = getmaxmumstep(arraytemp);
		int total = maxindex+privot+1;
		if (sourcearray[privot] == 0&&privot != sourcearray.length-1) {
//			System.out.print("Failure");
			resultsString = "Failure";
//			return resultsString;
		}
		else{
		if(privot == 0){
			resultsString = resultsString +privot+", ";
			//System.out.print(privot+", ");
		}
		int j = 0;
		for (int i = privot+1; i <= range; i++) {
			arraytemp[j] = sourcearray[i];
			j++;
		}
		
		if(total<sourcearray.length){
			resultsString = resultsString + total+", ";
			//System.out.print(total+", ");
			hopbrain(total,sourcearray[total], sourcearray);
		}
		else {
			resultsString = resultsString+"out";
//			System.out.print(resultsString);
//			return true;
//			return resultsString;
		}
		}
		return resultsString;
//		return false;
	}
	
	public static void main(String[] argv) throws IOException{
		int[] array = getfiles("/Users/songkai/Desktop/inputfile.txt");
		//getminhop(array);
		System.out.println(getminhop(array));
	}
	
	
	public static int getmaxmumstep(int[] inputarray){
		int maxdistance = 0;
		int index = 0;
		for (int i = 0; i < inputarray.length; i++) {
			int tempdistance = i+inputarray[i];
			if(maxdistance <= tempdistance){
				maxdistance = tempdistance;
				index = i;
			}
		}
		return index;
	}

}
